////////////////////////////////////////////////
//
// Author: Arkadiusz Kiciak (WITS)
// City: Gdansk POLAND
// Date: 18 May 2014
// License: GPLV3
//
////////////////////////////////////////////////
//
// Feel free to redistributing this code but never
// forget to add original place where you found this
// Thanks!!!
//
////////////////////////////////////////////////
//
// Project of electronic game based at
// finger reaction time - for two players
//
// Display was made from 8 led diodes on purpose
// Six of them are yellow and two red (YYYRRYYY)
//
// Red diodes are functional to provide current gameplay 
//
// Yellow diodes indicate how much win
// player was faster them opposite
//
////////////////////////////////////////////////
//
// How to play:
//
// > At the first turn players are preparing yourself
// when red diodes are blinking - signal get ready
//
// > When diodes stop blinking players await for
// continuously light signal from red diodes
//
// > When red diodes lighting every player should
// press his dedicated button
//
// Who is faster is winning!
//
// I wish you a good game - author
//
////////////////////////////////////////////////
//
// > "avr-objdump.exe" -h main.elf
//
// main.elf:     file format elf32-avr
//
// Sections:
// Idx Name          Size      VMA       LMA       File off  Algn
//   0 .text         00000732  00000000  00000000  00000094  2**1
//                   CONTENTS, ALLOC, LOAD, READONLY, CODE
//   1 .data         00000004  00800060  00000732  000007c6  2**0
//                   CONTENTS, ALLOC, LOAD, DATA
//   2 .bss          0000000f  00800064  00800064  000007ca  2**0
//                   ALLOC
//
// > Process Exit Code: 0
// > Time Taken: 00:00
//
////////////////////////////////////////////////

#define ledDisplay 				PORTA
#define ledDisplayConfigReg		DDRA

#define diode1					PA0
#define diode2					PA1
#define diode3					PA2

#define diode4					PA3
#define diode5					PA4

#define diode6					PA5
#define diode7					PA6
#define diode8					PA7

#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>

#include "commonProcs.h"
#include "keyConfig.h"

volatile uint8_t player1TimeCounting = 0;
volatile uint8_t player2TimeCounting = 0;

//2000 ms / 4.1 ms, count of interrupts
uint32_t maxTime = 488;

volatile uint8_t gameOver = 0;

volatile uint32_t player1Time = 0;
volatile uint32_t player2Time = 0;

void displayInit(void){
	
	ledDisplayConfigReg = 0xFF;
	
	ledDisplay = 0X00;
	
}

void displayResults(uint32_t p1, uint32_t p2){

	uint8_t diff = 0;
	
	double diffOfTime = 0;

	if(p1 == p2){
	
		ledDisplay = 0xFF;
		
		return;
		
	} else
	
		ledDisplay = 0x00;

	if(p1 < p2){
		
		setBitAt(ledDisplay,diode4);
		
		diff = p2 - p1;
		
		diffOfTime =
			(double)diff * 100.0 / (double)p2;

		if(diffOfTime >= 4){
		
			setBitAt(ledDisplay,diode3);
		
		}

		if(diffOfTime >= 12){
		
			setBitAt(ledDisplay,diode2);
		
		}
		
		if(diffOfTime >= 24){
			
			setBitAt(ledDisplay,diode1);

		}
	
	} else {
	
		setBitAt(ledDisplay,diode5);

		diff = p1 - p2;
	
		diffOfTime =
			(double)diff * 100.0 / (double)p1;

		if(diffOfTime >= 4){
		
			setBitAt(ledDisplay,diode6);
		
		}

		if(diffOfTime >= 12){
		
			setBitAt(ledDisplay,diode7);
		
		}
		
		if(diffOfTime >= 24){
		
			setBitAt(ledDisplay,diode8);
		
		}
		
	}

}

int main(void){

	displayInit();
	
	keyInit();

	setBitAt(ledDisplay,diode4);
	setBitAt(ledDisplay,diode5);

	uint16_t delayTime = 2000;
		
	while(delayTime >= 100){
	
		_delay_ms(100); delayTime -= 100;
				
		setBitDependOf(!(ledDisplay & _BV(diode4)),
			ledDisplay,diode4);

		setBitDependOf(!(ledDisplay & _BV(diode5)),
			ledDisplay,diode5);
		
	}
	
	clrBitAt(ledDisplay,diode4);
	clrBitAt(ledDisplay,diode5);
	
	_delay_ms(1000);

	setBitAt(ledDisplay,diode4);
	setBitAt(ledDisplay,diode5);
	
	player1TimeCounting =
	player2TimeCounting = 1;	

	sei();
	
	while(1){
			
		if(gameOver){
			
			displayResults(player1Time,player2Time);
			
			break;
			
		}
		
	}
	
	return 0;
	
}
